package com.example.himalayaproject.find.monthlyfocus.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.anchor.fragment.BaseApplication;
import com.example.himalayaproject.find.monthlyfocus.adapter.RecycleViewAdapter;
import com.example.himalayaproject.find.monthlyfocus.entity.HeadPage;
import com.example.himalayaproject.find.monthlyfocus.entity.Month_List;
import com.example.himalayaproject.find.monthlyfocus.entity.MonthlyArray;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Monthly_Focus_Fragment extends Fragment implements Callback<MonthlyArray>{
private RecyclerView recyclerView;

 public static Monthly_Focus_Fragment getInstance(){
        return new Monthly_Focus_Fragment();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_monthly__focus_, container, false);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //找到控件
        recyclerView= (RecyclerView) view.findViewById(R.id.recycler_frg_ranking_id);
        //下载数据

//        MyApplication application=(MyApplication)getActivity().getApplication();
//        application.getService()
//                .getMonthlyArray()
//                .enqueue(this);//绑定回调接口

        BaseApplication application= (BaseApplication) getActivity().getApplication();
        application.getHttpsService()
                .getMonthlyArray()
                .enqueue(this);
    }

    @Override
    public void onResponse(Call<MonthlyArray> call, Response<MonthlyArray> response) {
      //数据源
        //榜单信息数据源

        List<Month_List> datas=response.body().getDatas();
        //头部数据源信息
        HeadPage headPage=response.body().getFocusImages().getList().get(0);

        //绑定适配器
        RecycleViewAdapter adapter=new RecycleViewAdapter(getContext(),datas,headPage);
        recyclerView.setAdapter(adapter);
        //设置每个实体类之间的距离
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view)!=0) {
                    outRect.bottom = 30;
                }
            }
        });

    }
    @Override
    public void onFailure(Call<MonthlyArray> call, Throwable t) {
        t.printStackTrace();
      //  Toast.makeText(getActivity(), "数据请求失败", Toast.LENGTH_SHORT).show();
    }
}
